import 'package:dio/dio.dart';

///Dio的封装类
class ApiStrategy {
  static ApiStrategy? _instance;

  static final String baseUrl = "https://maize.yymimang.cn";
  static const int connectTimeOut = 20 * 1000; //连接超时时间为20秒
  static const int receiveTimeOut = 60 * 1000; //响应超时时间为60秒
  /// 是否打印请求日志
  static const LOGS_ENABLED = false;

  Dio? _client;

  static ApiStrategy? getInstance() {
    if (_instance == null) {
      _instance = ApiStrategy._internal();
    }
    return _instance;
  }

  ApiStrategy._internal() {
    if (_client == null) {
      BaseOptions options = new BaseOptions();
      options.connectTimeout = Duration(milliseconds: connectTimeOut);
      options.receiveTimeout = Duration(milliseconds: receiveTimeOut);
      options.baseUrl = baseUrl;
      _client = new Dio(options);
      // 添加日志打印拦截器, 放在最后面
      if (LOGS_ENABLED) {
        LogInterceptor logInterceptor = LogInterceptor(
          requestBody: true,
          responseBody: true,
        );
        _client!.interceptors.add(logInterceptor);
      }
    }
  }

  Dio? get client => _client;
  static const String GET = "get";
  static const String POST = "post";

  static String getBaseUrl() {
    return baseUrl;
  }

  //get请求
  void get(
    String url,
    Function callBack, {
    Map<String, String>? params,
    Function? errorCallBack,
    CancelToken? token,
  }) async {
    _request(
      url,
      callBack,
      method: GET,
      params: params,
      errorCallBack: errorCallBack,
      token: token,
    );
  }

  //post请求
  void post(
    String url,
    Function callBack, {
    Map<String, String>? params,
    Function? errorCallBack,
    CancelToken? token,
  }) async {
    _request(
      url,
      callBack,
      method: POST,
      params: params,
      errorCallBack: errorCallBack,
      token: token,
    );
  }

  //post请求
  void postUpload(
    String url,
    Function callBack,
    ProgressCallback progressCallBack, {
    FormData? formData,
    Function? errorCallBack,
    CancelToken? token,
  }) async {
    _request(
      url,
      callBack,
      method: POST,
      formData: formData,
      errorCallBack: errorCallBack,
      progressCallBack: progressCallBack,
      token: token,
    );
  }

  void _request(
    String url,
    Function callBack, {
    String? method,
    Map<String, String>? params,
    FormData? formData,
    Function? errorCallBack,
    ProgressCallback? progressCallBack,
    CancelToken? token,
  }) async {
    String errorMsg = "";
    int? statusCode;
    try {
      Response response;
      if (method == GET) {
        //组合GET请求的参数
        if (params != null && params.isNotEmpty) {
          response = await _client!.get(
            url,
            queryParameters: params,
            cancelToken: token,
          );
        } else {
          response = await _client!.get(
            url,
            cancelToken: token,
          );
        }
      } else {
        if (params != null && params.isNotEmpty || formData != null) {
          response = await _client!.post(
            url,
            data: formData ?? new FormData.fromMap(params!),
            onSendProgress: progressCallBack,
            cancelToken: token,
          );
        } else {
          response = await _client!.post(
            url,
            cancelToken: token,
          );
        }
      }
      statusCode = response.statusCode;
      //处理错误部分
      if (statusCode! < 0) {
        errorMsg = "网络请求错误,状态码:" + statusCode.toString();
        _handError(errorCallBack, errorMsg);
        return;
      }
      callBack(response.data);
    } catch (e) {
      _handError(errorCallBack, e.toString());
    }
  }

  //处理异常
  static void _handError(Function? errorCallback, String errorMsg) {
    if (errorCallback != null) {
      errorCallback(errorMsg);
    }
  }
}
